package com.zretc.emptraining.daoimpl;

import java.util.List;

import com.zretc.emptraining.dao.TeachDao;
import com.zretc.emptraining.dbtools.DBTools;
import com.zretc.emptraining.dbtools.PageData;
import com.zretc.emptraining.entity.Teach;

public class TeachDaoImpl implements TeachDao {

	@Override
	public boolean addTeach(Teach teach) {
		if(teach != null) {
			String sql = "insert into teach values(null,?,?,?,?,?,?)";
			return DBTools.getUpdate(sql, teach.getTeacherName(),teach.getTeacherSex(),teach.getTeacherXl(),
					teach.getTeacherTel(),teach.getTeacherDesc(),teach.getTeacherPic()) > 0;
		}
		return false;
	}

	@Override
	public boolean delTeachByTeachNo(int teachno) {
		if(teachno > 0) {
			String sql = "delete from teach where teacherNo = ?";
			return DBTools.getUpdate(sql, teachno) > 0;
		}
		return false;
	}

	@Override
	public boolean updTeach(Teach teach) {
		if(teach != null) {
			String sql = "update teach set teacherPic = ? where teacherNo = ?";
			return DBTools.getUpdate(sql, teach.getTeacherPic(),teach.getTeacherNo()) > 0;
		}
		return false;
	}

	@Override
	public boolean updInfo(Teach teach) {
		if(teach != null) {
			String sql = "update teach set teacherName = ?,teacherSex = ?,teacherXl = ?,teacherTel = ?,teacherDesc = ? where teacherNo = ?";
			return DBTools.getUpdate(sql, teach.getTeacherName(),teach.getTeacherSex(),teach.getTeacherXl(),teach.getTeacherTel(),
					teach.getTeacherDesc(),teach.getTeacherNo()) > 0;
		}
		return false;
	}
	
	@Override
	public Teach getTeachByTeacheName(String teachername) {
		String sql = "select * from teach where teacherName = ?";
		List<Teach> list = (List<Teach>) DBTools.exQuery(sql, Teach.class, teachername);
		if(list.size() == 0) {
			return null;
		}
		return list.get(0);
	}
	/**
	 * 分页查询
	 */

	@Override
	public PageData<Teach> getPageByteachName(int page , int pageSize , String keyword) {
		String sql = "select * from teach where teacherName like ?";
		PageData<Teach> pd = DBTools.exQueryByPage(sql, Teach.class, page, pageSize, "%"+keyword+"%");
		return pd;
	}
	
	@Override
	public PageData<Teach> getPageByteachNo(int page, int pageSize, String keyword) {
		String sql = "select * from teach where teacherNo = ?";
		PageData<Teach> pd = DBTools.exQueryByPage(sql, Teach.class, page, pageSize, keyword);
		return pd;
	}
	
	@Override
	public PageData<Teach> getPageByteachSex(int page, int pageSize, String keyword) {
		String sql = "select * from teach where teacherSex = ?";
		PageData<Teach> pd = DBTools.exQueryByPage(sql, Teach.class, page, pageSize, keyword);
		return pd;
	}
	
	@Override
	public PageData<Teach> getPageByteachXl(int page, int pageSize, String keyword) {
		String sql = "select * from teach where teacherXl = ?";
		PageData<Teach> pd = DBTools.exQueryByPage(sql, Teach.class, page, pageSize, keyword);
		return pd;
	}
	
	/**
	 * 获得所有教员的信息
	 * @return List<Teach>
	 */
	@Override
	public List<Teach> getAllTeachers() {
		String sql = "select * from teach ";
		List<Teach> list=(List<Teach>)DBTools.exQuery(sql, Teach.class);
		return list;
	}
	
	@Override
	public List<Teach> getTeachByTel(String teachtel) {
		String sql = "select * from teach where teacherTel = ?";
		return (List<Teach>) DBTools.exQuery(sql, Teach.class, teachtel);
	}
	
	@Override
	public boolean updInfoByDesc(String Tel, String desc, String teacherPic) {
		String sql = "update teach set teacherDesc = ?,teacherPic=? where teacherTel = ?";
		return DBTools.getUpdate(sql,  desc,teacherPic,Tel) > 0;
	}
	
}
